# import requests
#
#
#
# page = 2
# url = f'https://www.qimao.com/qimaoapi/api/classify/book-list?channel=0&category1=202&category2=a&words=a&update_time=a&is_vip=a&is_over=1&order=click&page={page}'
#
#
# headers = {
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
#     'cookie':'acw_sc__v2=197d84838-b72a4f730e1520f97c0b3757c1919c3d4de155b23a123f6d40; acw_tc=0aef82e417625824714581044e01c7a17a4046f8f82b0efa32e8d563a41529; Hm_lvt_1b6d0fc94c391c78c2fbeda715896432=1762582475; HMACCOUNT=13BAD7746936B6C2; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2219a621a31996ac-01efb4b5c375801-26061b51-1821369-19a621a319a1ed3%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTlhNjIxYTMxOTk2YWMtMDFlZmI0YjVjMzc1ODAxLTI2MDYxYjUxLTE4MjEzNjktMTlhNjIxYTMxOWExZWQzIn0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%2C%22%24device_id%22%3A%2219a621a31996ac-01efb4b5c375801-26061b51-1821369-19a621a319a1ed3%22%7D; sajssdk_2015_cross_new_user=1; Hm_lpvt_1b6d0fc94c391c78c2fbeda715896432=1762582548',
# }
# response = requests.get(url, headers=headers)
#
# if response.status_code == 200:
#     # print(response.text)
#     json_data = response.json()['data']
#     print(json_data)
#     items = json_data['book_list']
#     # print(items)
#     for item in items:
#         book_id = item['book_id']
#         bookName = item['title']
#
#         list_url = f"https://www.qimao.com/qimaoapi/api/book/chapter-list?book_id={book_id}"
#         # print(bookName)
#         # res = requests.get(list_url, headers=headers)
#         # if res.status_code == 200:
#         #     dt_json = res.json()['data']
#         #     chapters = dt_json['chapters']
#         #     for chapter in chapters:
#         #         dit = {
#         #             '目录id': chapter['id'],
#         #             '章节名': chapter['title'],
#         #         }
#         #         # chapterId = chapter['id']
#         #         # chapterName = chapter['title']
#         #         print(dit)
#         #         break
#         #     break
#


import requests

page = 2
url = f'https://www.qimao.com/qimaoapi/api/classify/book-list?channel=0&category1=202&category2=a&words=a&update_time=a&is_vip=a&is_over=1&order=click&page={page}'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
    'cookie': 'acw_sc__v2=197d84838-b72a4f730e1520f97c0b3757c1919c3d4de155b23a123f6d40; acw_tc=0aef82e417625824714581044e01c7a17a4046f8f82b0efa32e8d563a41529; Hm_lvt_1b6d0fc94c391c78c2fbeda715896432=1762582475; HMACCOUNT=13BAD7746936B6C2; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2219a621a31996ac-01efb4b5c375801-26061b51-1821369-19a621a319a1ed3%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTlhNjIxYTMxOTk2YWMtMDFlZmI0YjVjMzc1ODAxLTI2MDYxYjUxLTE4MjEzNjktMTlhNjIxYTMxOWExZWQzIn0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%2C%22%24device_id%22%3A%2219a621a31996ac-01efb4b5c375801-26061b51-1821369-19a621a319a1ed3%22%7D; sajssdk_2015_cross_new_user=1; Hm_lpvt_1b6d0fc94c391c78c2fbeda715896432=1762582548',
}

response = requests.get(url, headers=headers)
if response.status_code == 200:
    try:
        json_data = response.json()['data']
    except ValueError:
        print('返回不是 JSON，原始内容：', response.text[:500])
        exit()
    # json_data = response.json()['data']
    items = json_data['book_list']
    for item in items:
        book_id = item['book_id']
        bookName = item['title']
        print(f'📖 {bookName}（{book_id}）')

        list_url = f"https://www.qimao.com/qimaoapi/api/book/chapter-list?book_id={book_id}"
        res = requests.get(list_url, headers=headers)
        if res.status_code == 200:
            dt_json = res.json()['data']
            chapters = dt_json['chapters']
            for chapter in chapters:
                print(f"  ├─ {chapter['title']}（{chapter['id']}）")
        break  # 只测一本